﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace DeepZoomPivotConstructor.PivotLib
{
    /// <summary>
    /// A collection of images - that can be written out to be read in by the pivot guys
    /// </summary>
    public class PivotCollection : IEnumerable<PivotImage>
    {
        /// <summary>
        /// Holds onto the list if images.
        /// </summary>
        private List<PivotImage> _images = new List<PivotImage>();

        /// <summary>
        /// Get the facet catagories
        /// </summary>
        public IndexedProperty<string, FacetCategoryInfo> FacetCatagories { get; private set; }

        private Dictionary<string, FacetCategoryInfo> _catagories = new Dictionary<string, FacetCategoryInfo>();

        public PivotCollection()
        {
            FacetCatagories = new IndexedProperty<string, FacetCategoryInfo>(
                s => _catagories[s], (s, v) => _catagories[s] = v
                    );
        }

        /// <summary>
        /// Add a Pivot image that we "track".
        /// </summary>
        /// <param name="img"></param>
        public void Add(PivotImage img)
        {
            _images.Add(img);
            ScanFacets(img);
        }

        /// <summary>
        /// Looks at the image and extracts the facets and if we don't know
        /// about a facet, then add it to the list.
        /// </summary>
        /// <param name="img"></param>
        private void ScanFacets(PivotImage img)
        {
            var newfac = from f in img
                         select f.Name;
            foreach (var item in newfac)
            {
                AddCategory(item);
            }
        }

        /// <summary>
        /// Adds a new catagory to the internal set of categories.
        /// </summary>
        /// <param name="item"></param>
        public void AddCategory(string item)
        {
            if (!_catagories.ContainsKey(item))
            {
                _catagories.Add(item, new FacetCategoryInfo());
            }
        }

        /// <summary>
        /// Returns the list of items that we are looking at...
        /// </summary>
        /// <returns></returns>
        public IEnumerator<PivotImage> GetEnumerator()
        {
            return _images.GetEnumerator();
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            throw new NotImplementedException();
        }
    }
}
